<?php


namespace app\admin\controller;

use app\admin\model\Bclass as BclassModel;
use app\admin\model\Sclass as SclassModel;
use think\Controller;
class Bclass extends Controller
{
    //调用添加一级分类页面
    function addbclass(){
       return $this->fetch();
    }

    //添加一级分类
    function doadd(){
        $data=$this->request->param();
        $model=new BclassModel();
        $res=$model->save($data);
        if($res){
            return alert_success('添加成功','Bclass/listbclass');
        }else{
            return alert_success('添加分类失败，请检测输入!');
        }

        $file = $this->upload();
        if($file){
             $model->addImg($file);
        }else{
            $this->error('上传分类图片失败，请重新添加!');
        }

$model=new BclassModel();
        $bclass =$model->save($_POST);
        if($bclass){
           return alert_success('添加分类成功','Bclass/listbclass');
        }else{
           return alert_error('添加分类失败!');
        }

    }

    //查询一级分类信息
    function listbclass(){
        $model=new BclassModel();
        $bdata = $model->getBinfo();
        $this->assign('bdata',$bdata);
       return $this->fetch();
    }
    public function editbclass($no){
        $model=new BclassModel();
        $bclass=$model->getOne($no);
        $this->assign('bdata',$bclass);
        return $this->fetch();
    }

    public function doedit(){
        $data=$this->request->param();
        $model=new BclassModel();
        $res=$model->updateBclass($data);
        if ($res) {
            return alert_success('修改成功','Bclass/listbclass');
        } else {
            return alert_error('修改失败');
        }
    }
    //删除一级分类
    function del($no){
        $model=new BclassModel();
$model1=new SclassModel();
        $res = $model1->getSclass($no);
        if(!empty($res)){
            return alert_error('该分类下存在二级分类,不允许删除该分类!');exit;
        }else{
        if($model->delBclass($no)){
            return alert_success('删除分类成功','Bclass/listbclass');
        }else{
            return alert_error('删除分类失败');
        }
    }}
}